import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';

import '../../theme/theme_color.dart';

class PhotoTool {
  static void getPhotoUrl(String type, BuildContext context,
      {Function? callBack, bool isNoNeedCrop = false}) async {
    final picker = ImagePicker();
    ImageSource source;
    if (type == '拍照') {
      source = ImageSource.camera;
    } else {
      source = ImageSource.gallery;
    }
    final pickerFile = await picker.pickImage(source: source, imageQuality: 50);
    getPhotoResultUrl(pickerFile,
        callBack: callBack, isNoNeedCrop: isNoNeedCrop);
  }

  static void getPhotoImgFile(String type, BuildContext context,
      {Function? callBack, bool isNoNeedCrop = false}) async {
    final picker = ImagePicker();
    ImageSource source;
    if (type == '拍照') {
      source = ImageSource.camera;
    } else {
      source = ImageSource.gallery;
    }
    final pickerFile = await picker.pickImage(source: source, imageQuality: 50);
    getPhotoResultUrl(pickerFile,
        callBack: callBack, isNoNeedCrop: isNoNeedCrop);
  }

  static void getPhotoResultUrl(pickerFile,
      {Function? callBack, bool isNoNeedCrop = false}) async {
    if (pickerFile != null) {
      if (isNoNeedCrop) {
        String filepath = pickerFile.path;
        if (callBack != null) {
          callBack(filepath);
        }
      } else {
        CroppedFile? croppedImage = await ImageCropper().cropImage(
            sourcePath: pickerFile.path,
            //图片资源
            maxHeight: 500,
            maxWidth: 500,
            aspectRatio: const CropAspectRatio(ratioX: 1, ratioY: 1),
            //裁剪框比例
            uiSettings: [
              AndroidUiSettings(
                  toolbarTitle: '裁剪', lockAspectRatio: true //锁定图片纵横比
                  ),
              IOSUiSettings(
                  title: '裁剪',
                  resetButtonHidden: true,
                  //不显示重置按钮
                  aspectRatioLockEnabled: true,
                  //锁定图片纵横比
                  aspectRatioPickerButtonHidden: true,
                  //隐藏切换纵横比按钮
                  doneButtonTitle: "完成",
                  //confirm按钮文字
                  cancelButtonTitle: "取消" //cancel按钮文字
                  )
            ]); //根据XFile对象的路径获得图片进行裁剪
        //裁剪完成
        if (croppedImage != null) {
          //裁剪后图片的路径
          String filepath = croppedImage.path;
          if (callBack != null) {
            callBack(filepath);
          }
          print("filepath-----$filepath");
        }
      }
    }
  }

  static void selectPhotoFromSheet(context,
      {bool isCover = true, Function? callBackUrl}) async {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return SizedBox(
            height: 220.0,
            child: Container(
                alignment: Alignment.center,
                child: Column(
                  children: [
                    GestureDetector(
                        onTap: () {
                          Navigator.pop(context);
                          getPhotoImgFile('拍照', context, callBack: (url) {
                            if (callBackUrl != null) {
                              callBackUrl(url);
                            }
                          }, isNoNeedCrop: true);
                        },
                        child: Container(
                          alignment: Alignment.center,
                          // width: 1.sw,
                          height: 55,
                          color: Colors.white,
                          child: const Text('拍照'),
                        )),
                    Divider(
                      color: ThemeColor.sendMsgColor,
                      height: 1,
                    ),
                    GestureDetector(
                        onTap: () {
                          Navigator.pop(context);
                          getPhotoImgFile('相册', context, callBack: (url) {
                            if (callBackUrl != null) {
                              callBackUrl(url);
                            }
                          }, isNoNeedCrop: true);
                        },
                        child: Container(
                          alignment: Alignment.center,
                          // width: 1.sw,
                          height: 60,
                          color: Colors.white,
                          child: const Text('从手机相册选择'),
                        )),
                    Divider(
                      color: ThemeColor.sendMsgColor,
                      thickness: 7,
                      height: 1,
                    ),
                    GestureDetector(
                      onTap: () {
                        Navigator.pop(context);
                      },
                      child: Container(
                        alignment: Alignment.center,
                        width: 1.sw,
                        height: 55,
                        child: const Text('取消'),
                      ),
                    ),
                  ],
                )));
      },
    );
  }
}
